﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Data;
using System.Data.Objects;

namespace RL
{
    /// <summary>
    /// Логика взаимодействия для ucScheduleRL.xaml
    /// </summary>
    public partial class ucScheduleRL : UserControl
    {



        public routelistEntities ContextDB
        {
            get 
            { 
                return (routelistEntities)GetValue(ContextDBProperty);
            }
            set
            {
                SetValue(ContextDBProperty, value);
            }
        }

        // Using a DependencyProperty as the backing store for ContextDB.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ContextDBProperty =
            DependencyProperty.Register("ContextDB", typeof(routelistEntities), typeof(ucScheduleRL), new UIPropertyMetadata(null, new PropertyChangedCallback(propContext)));

        public static void propContext(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ucScheduleRL item = d as ucScheduleRL;
            if (null == item) return;
            item.InitData();
        }

        public RouteList RouteList
        {
            get
            { 
                return (RouteList)GetValue(RouteListProperty); 
            }
            set 
            { 
                SetValue(RouteListProperty, value);
                InitData();
            }
        }

        // Using a DependencyProperty as the backing store for RouteList.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RouteListProperty =
            DependencyProperty.Register("RouteList", typeof(RouteList), typeof(ucScheduleRL), new UIPropertyMetadata(null, new PropertyChangedCallback(propRL)));

        public static void propRL(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ucScheduleRL item = d as ucScheduleRL;
            if (null == item) return;
            item.InitData();
        }

        private IEnumerable<RouteListSchedule> getQuery(routelistEntities routelistEntities, RouteList rl)
        {
            if ((routelistEntities == null) || (rl == null)) return null;
            IEnumerable<RouteListSchedule> query = from o in routelistEntities.RouteListSchedule where o.id_rl ==rl.id select o;
            return query;
        }

        private IEnumerable<Routes> getQueryRoutes(routelistEntities routelistEntities)
        {
            if (null == routelistEntities) return null;
            IEnumerable<Routes> query = from o in routelistEntities.Routes orderby o.number where o.del == false select o;
            return query;
        }  

        public ObjectResult<RouteListSchedule> RLS
        {
            get { return (ObjectResult<RouteListSchedule>)GetValue(RLSProperty); }
            set { SetValue(RLSProperty, value); }
        }

        // Using a DependencyProperty as the backing store for RLS.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RLSProperty =
            DependencyProperty.Register("RLS", typeof(ObjectResult<RouteListSchedule>), typeof(ucScheduleRL), new UIPropertyMetadata(null));

        
        public ucScheduleRL()
        {
            InitializeComponent();
        }

        private void buttonAddSchedule_Click(object sender, RoutedEventArgs e)
        {
            if (null == RouteList || null == ContextDB) return;
            CollectionViewSource cvs = (CollectionViewSource)this.FindResource("sourceSchedule");
            IEditableCollectionView vw = (IEditableCollectionView)cvs.View;
            RouteListSchedule item = vw.AddNew() as RouteListSchedule;
            if (null == item) return;
            item.id_rl = RouteList.id;
            vw.CommitNew();
            cvs.View.MoveCurrentTo(item);
            listViewSchedule.ScrollIntoView(item);
        }

        private void buttonDeleteSchedule_Click(object sender, RoutedEventArgs e)
        {
            if (ContextDB == null) return;
            CollectionViewSource cvs = (CollectionViewSource)this.FindResource("sourceSchedule");
            RouteListSchedule item = cvs.View.CurrentItem as RouteListSchedule;
            if (null == item) return;
            ContextDB.DeleteObject(item);
        }

        private void buttonEditSchedule_Click(object sender, RoutedEventArgs e)
        {

        }

        private void InitData()
        {
            if (null == ContextDB) return;
            if (null == RouteList) return;
            CollectionViewSource cvs = (CollectionViewSource)this.FindResource("sourceSchedule");
            cvs.Source = getQuery(ContextDB, RouteList);
            CollectionViewSource cvsRoutes = (CollectionViewSource)this.FindResource("sourceRoutes");
            cvsRoutes.Source = getQueryRoutes(ContextDB);
        }
    }
}
